package iwb.reference.cacheaop;

import iwb.utils.RedisUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RBucket;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


@Component
@Aspect
public class CacheAspect {

    @Autowired
    RedisUtils redisUtils;

    @Pointcut("@annotation(iwb.reference.cacheaop.annotation.GuavaCacheAble)")
    private void matchAbsCache(){
        throw new UnsupportedOperationException();
    }


    @Pointcut("@annotation(iwb.reference.cacheaop.annotation.RedisCacheAble)")
    private void matchRedisCache(){
        throw new UnsupportedOperationException();
    }

    /**
     * around.
     *
     * @param joinPoint ProceedingJoinPoint
     *
     * @return Object
     */
    @Around("matchAbsCache()")
    public Object around(ProceedingJoinPoint joinPoint) {
        MethodInvocationProceedingJoinPoint methodInvocationProceedingJoinPoint = (MethodInvocationProceedingJoinPoint)joinPoint;
        Method pointMethod = ((MethodSignature)methodInvocationProceedingJoinPoint.getSignature()).getMethod();
        AbstractCache executeObject = (AbstractCache) joinPoint.getThis();
        String cachekey = Utils.getCacheKey(joinPoint.getSignature().getName(), joinPoint.getArgs());
        return executeObject.setCacheGetCallable(joinPoint.getTarget(), cachekey, pointMethod,joinPoint.getArgs());
    }



//    @Around("matchRedisCache()")
//    public Object aroundRedis(ProceedingJoinPoint joinPoint) {
//        MethodInvocationProceedingJoinPoint methodInvocationProceedingJoinPoint = (MethodInvocationProceedingJoinPoint)joinPoint
//        Method pointMethod = ((MethodSignature)methodInvocationProceedingJoinPoint.getSignature()).getMethod()
//        String key = Utils.getCacheKey(joinPoint.getSignature().getName(),joinPoint.getTarget().getClass().getCanonicalName(), joinPoint.getArgs())
//        RBucket<String> keyObject = redisUtils.getRedissonClient().getBucket(key)
//        keyObject.e
//        AbstractCache executeObject = (AbstractCache) joinPoint.getThis()
//        String cachekey = Utils.getCacheKey(joinPoint.getSignature().getName(), joinPoint.getArgs())
//        return executeObject.setCacheGetCallable(joinPoint.getTarget(), cachekey, pointMethod)
//        return null
//    }
}
